module ApplicationHelper
	
	include Permissible
	
	# Returns the full title on a per-page basis.
	def title(page_title="")
		base_title = "Uni Ulm DMS"
		if page_title.empty?
			base_title
		else
			"#{base_title} | #{page_title}"
		end
	end
	
	# Returns the name of the currently active menu tab.
	def current_tab_name(controller=params[:controller], action=params[:action])
		case controller
		when 'categories'
			'archive'
		when 'documents'
			if action == 'new'
				'home'
			else				
				'archive'
			end
		else
			'home'
		end
	end
	
	# Returns the menu entries for the home tab.
	def home_tab_menu_entries
		# Return immediately when this is called on a guest, since he has no edit_user_path
		# This happens since this method will be called before any redirects like when accessing
		# the home action in the StaticPagesController.
		return [] if current_user.guest?

		[
			[t('navigation.news'), home_path],
			[t('navigation.create_document'), '/documents/new'],
			[t('navigation.groups'), '#'],
			[t('navigation.my_documents'), '#'],
			[t('navigation.personal_data'), edit_user_path(current_user)],
			[t('navigation.preferences'), '#']
		]
	end
	
	# Checks whether the given category is the currently selected page or if an descendant of it is
	# currently selected, in which case the given category needs to be selected itself, too.
	def selected_category?(category)
		is_ancestor = false
		unless params[:id].blank?
			if params[:controller] == 'documents' 
				cat_id = Document.find(params[:id]).categories.first.id
			else
				cat_id = params[:id]
			end
			current_category = Category.find(cat_id)
			is_ancestor = current_category.ancestor_ids.include?(category.id) # is it an ancestor category?
			is_ancestor ||= current_category.id == category.id # or does document belong to this category?
		end
		current_page?(category) or is_ancestor
	end
	
	# Returns an array of the latest created documents for the sidebar.
	def latest_created_documents
		Document.order('documents.created_at DESC').limit(5)
	end
	
	# Returns the given items adequately indented by their ancestry depth.
	def indented_options(items, &block)
		return indented_options(items) { |i| "#{'&nbsp;' * 4 * i.depth}#{i.name}".html_safe } unless block_given?

		result = []
		items.map do |item, sub_items|
			result << [yield(item), item.id]
			#this is a recursive call:
			result += indented_options(sub_items, &block)
		end
		result
	end
	
	# Converts the given user ID to a link to the corresponding user.
	def id_to_user_link(id)
		unless id =~ /[0-9]/
			return id
		end
		user = User.find(id)
		link_to user.name, user
	end
	
	# Helper method to check permissions in views
	def has_permission_for?(namespace, action, resource_id=nil)
		permissible?(action, resource_id, namespace)
	end
	
	# Handy function that checks for edit and destroy rights
	def show_admin_options?(namespace, resource_id=nil)
		permissible?('edit', resource_id, namespace) or permissible?('destroy', resource_id, namespace)
	end
	
end
